package com.tnr.scgcxx.controller.safty;

import java.io.InputStream;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.tnr.scgcxx.base.Constants;
import com.tnr.scgcxx.base.CurrUser;
import com.tnr.scgcxx.base.Result;
import com.tnr.scgcxx.dto.UserDto;
import com.tnr.scgcxx.exception.BusinessException;
import com.tnr.scgcxx.exception.SysException;
import com.tnr.scgcxx.service.safty.LoginService;

@RestController
@RequestMapping("/safty/login")
//@SessionAttributes(Constants.SESSION_ATTR_CURR_USER)//说明什么属性存储于session中
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	@PostMapping("/user") //可以使用postman测试
	public Result login(@RequestBody UserDto dto,/*Model model*/  HttpSession session ) {
		
		try {
			CurrUser currUser = loginService.checkLogin(dto);//返回当前用户
			
			//model默认在request中存放信息
			//model.addAttribute(Constants.SESSION_ATTR_CURR_USER, currUser);
			session.setAttribute(Constants.SESSION_ATTR_CURR_USER, currUser);
			
			return Result.success("登录成功！");
			
		} catch (BusinessException e) {
			
			return Result.fail(e.getMessage());
			
		} catch(SysException e) {
			
			e.printStackTrace();
			return Result.fail("系统升级中......");
			
		}
		
		
		
		
		
	}
	
	
	public void kkk(@RequestParam("kkk")  MultipartFile file) {
		//InputStream in = file.getInputStream();
		
		
	}

}
